package henu.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import henu.bean.Communictor;
import henu.bean.TClass;
import henu.dao.ICommunictorDao;
import henu.factory.DaoFactory;
import henu.util.Dhcp;

public class CommunictorDaoImpl implements ICommunictorDao {
	protected QueryRunner qr =  null;
	
	public CommunictorDaoImpl(QueryRunner queryRunner) {
		super();
		this.qr = queryRunner;
	}
	@Override
	public boolean login(String name, String pwd) {
		qr = DaoFactory.queryRunner;
		String sql = "select * from tb_communictor where fName=? and fPassword=?";
		Object[] param = {name,pwd};
		try {
			if(qr.query(sql, param, new BeanHandler<Communictor>(Communictor.class))!=null){
				return true;
			}else{
				return false;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	@Override
	public int save(Communictor comm) {
		qr = new QueryRunner(Dhcp.getDataSource());
		String sql = "insert into tb_communictor(fId,fName,fPassword,fClass,fQQ,fEmail,fPhone,fMianSubject,fMemo) values(?,?,?,?,?,?,?,?,?)";
		Object[] param = {comm.getfId(),comm.getfName(),comm.getfPassword(),comm.getfClass(),comm.getfQQ(),comm.getfEmail(),comm.getfPhone(),comm.getfMianSubject(),comm.getfMemo()};
		int n = 0;
		try {
			n = qr.update(sql, param);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return n;
	}

	@Override
	public int delete(String fId) {
		qr = new QueryRunner(Dhcp.getDataSource());
		String sql = "delete from tb_communictor where fId = ?";
		int n = 0;
		try {
			n = qr.update(sql, fId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return n;
	}

	@Override
	public int update(String fId, Communictor comm) {
		qr = new QueryRunner(Dhcp.getDataSource());
		String sql = "update tb_communictor comm set comm.fName=?,comm.fPassword=?,comm.fClass=?,comm.fQQ=?,comm.fEmail=?,comm.fPhone=?,comm.fMianSubject=?,comm.fMemo=? where fId=?";
		Object[] param = {comm.getfName(),comm.getfPassword(),comm.getfClass(),comm.getfQQ(),comm.getfEmail(),comm.getfPhone(),comm.getfMianSubject(),comm.getfMemo(),fId};
		int n = 0;
		try {
			n = qr.update(sql, param);
			System.out.println(n+"行已经发生改变");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return n;
	}

	@Override
	public List<Communictor> select(String province,String value) {
		qr = new QueryRunner(Dhcp.getDataSource());
		String sql1 = "select * from tb_communictor where fName=?";
		String sql2= "select * from tb_communictor where fMianSubject=?";
		List<Communictor> comm = null;
		if(province.equals("按姓名"))
		{try {
			System.out.println("loading");
			comm = qr.query(sql1, value,new BeanListHandler<Communictor>(Communictor.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
		if(province.equals("按课程"))
		{try {
			comm = qr.query(sql2, value,new BeanListHandler<Communictor>(Communictor.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
		return comm;
		
	}

	@Override
	public List<Communictor> getAll() {
		QueryRunner qr = new QueryRunner(Dhcp.getDataSource());
		String sql = "select * from tb_communictor";
		List<Communictor> comms = null;		
		try {
			comms = qr.query(sql, new BeanListHandler<Communictor>(Communictor.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return comms;
	}
	@Override
	public Communictor get(String fId) {
		// TODO Auto-generated method stub
		String sql="select * from tb_communictor where fId=?";
		Object param[]={fId};
		QueryRunner qr = new QueryRunner(Dhcp.getDataSource());
		Communictor communictor =null;
		try {
			communictor=qr.query(sql, param, new BeanHandler<Communictor>(Communictor.class));
			System.out.println("get");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return communictor;
	}
	
}
